package hethong;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import net.miginfocom.swing.MigLayout;
import javax.swing.JTextField;
import javax.swing.JComboBox;

import dangnhap.FromDangNhap;
import form.DSJ_NhanVien;
import form.DSJ_VaiTroQL;

public class ThemNguoiDung extends JDialog implements ActionListener {

	private final JPanel contentPanel = new JPanel();
	private JPanel plNorth,plCenter,plSouth;
	private JLabel lblNhanVien;
	private JLabel lblTaiKhoan;
	private JTextField txtTaiKhoan;
	private JLabel lblMatKhau;
	private JTextField txtMatKhau;
	private JLabel lblXacNhan;
	private JTextField txtXacNhan;
	private JLabel lblVaiTro;
	private JLabel lblDienGiai;
	private JTextField txtDienGiai;
	private JButton btnLuu,btnThoat;
	private JComboBox cmbNhanVien;
	private JComboBox cmbVaiTro;
	private JLabel lblAnh;
	public static void main(String[] args) {
		try {
			ThemNguoiDung dialog = new ThemNguoiDung();
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	public ThemNguoiDung() {
		FromDangNhap.rootPath = this.getClass().getResource("/").getPath();
		this.setTitle("Thêm người dùng");
		java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
	    setBounds((screenSize.width-277)/2, (screenSize.height-269)/2, 277, 268);
	    Image icon = Toolkit.getDefaultToolkit().getImage(FromDangNhap.rootPath+"images/TrangChu.gif");
        this.setIconImage(icon);
		//setBounds(100, 100, 277, 269);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(new BorderLayout(10, 10));
		plNorth = new JPanel();
		getContentPane().add(plNorth,BorderLayout.NORTH);
		plNorth.setLayout(new FlowLayout(FlowLayout.LEFT));
		
		lblAnh = new JLabel(new ImageIcon(FromDangNhap.rootPath+"images/sua.gif"));
		plNorth.add(lblAnh);
		
		JLabel lblTTBatBuoc = new JLabel("Thông tin bắt buộc");
		lblTTBatBuoc.setForeground(Color.blue);
		plNorth.add(lblTTBatBuoc);
		
		plCenter = new JPanel();
		getContentPane().add(plCenter,BorderLayout.CENTER);
		plCenter.setLayout(new MigLayout("", "[][grow]", "[][][][][][]"));
		
		lblNhanVien = new JLabel("Nhân Viên");
		plCenter.add(lblNhanVien, "cell 0 0,alignx left,aligny baseline");
		
		cmbNhanVien = new JComboBox();
		plCenter.add(cmbNhanVien, "cell 1 0,growx");
		
		lblTaiKhoan = new JLabel("Tài Khoản");
		plCenter.add(lblTaiKhoan, "cell 0 1,alignx left");
		
		txtTaiKhoan = new JTextField();
		plCenter.add(txtTaiKhoan, "cell 1 1,growx");
		txtTaiKhoan.setColumns(10);
		
		lblMatKhau = new JLabel("Mật Khẩu");
		plCenter.add(lblMatKhau, "cell 0 2,alignx left");
		
		txtMatKhau = new JTextField();
		plCenter.add(txtMatKhau, "cell 1 2,growx");
		txtMatKhau.setColumns(10);
		
		lblXacNhan = new JLabel("Xác Nhận");
		plCenter.add(lblXacNhan, "cell 0 3,alignx left");
		
		txtXacNhan = new JTextField();
		plCenter.add(txtXacNhan, "cell 1 3,growx");
		txtXacNhan.setColumns(10);
		
		lblVaiTro = new JLabel("Vai Trò");
		plCenter.add(lblVaiTro, "cell 0 4,alignx left");
		
		cmbVaiTro = new JComboBox();
		plCenter.add(cmbVaiTro, "cell 1 4,growx");
		
		lblDienGiai = new JLabel("Diễn Giải");
		plCenter.add(lblDienGiai, "cell 0 5,alignx left");
		
		txtDienGiai = new JTextField();
		plCenter.add(txtDienGiai, "cell 1 5,growx");
		txtDienGiai.setColumns(10);
		
		plSouth = new JPanel();
		plSouth.setLayout(new FlowLayout(FlowLayout.RIGHT));
		getContentPane().add(plSouth,BorderLayout.SOUTH);
		
		btnLuu = new JButton("Lưu",new ImageIcon(FromDangNhap.rootPath+"images/luu.gif"));
		plSouth.add(btnLuu);
		
		btnThoat = new JButton("Thoát",new ImageIcon(FromDangNhap.rootPath+"images/xoa1.gif"));
		plSouth.add(btnThoat);
		
		//sukien
		btnLuu.addActionListener(this);
		btnThoat.addActionListener(this);
		cmbNhanVien.addActionListener(this);
		cmbVaiTro.addActionListener(this);
		DbselectNhanVien();
		DbselectVaiTro();
	}
	public void DbselectVaiTro(){
		DSJ_VaiTroQL data = new DSJ_VaiTroQL();
		Vector<DSJ_VaiTroQL> getquanly = data.getVaiTroQL();
		for(int i=0;i<getquanly.size();i++){
			cmbVaiTro.addItem(getquanly.get(i).getVaiTro());
		}
	}
	public void DbselectNhanVien(){
		DSJ_NhanVien data = new DSJ_NhanVien();
		Vector<DSJ_NhanVien> getnhanvien = data.getMuaBan();
		for(int i=0;i<getnhanvien.size();i++){
			cmbNhanVien.addItem(getnhanvien.get(i).getHoTenNV());
		}
	}
	
	
	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource() == btnLuu){
			txtDienGiai.setText("");
			txtMatKhau.setText("");
			txtTaiKhoan.setText("");
			txtXacNhan.setText("");
			
		}
		if(e.getSource() == btnThoat){
			dispose();
		}
	}

}
